<template>
  <el-upload
    class="avatar-uploader"
    action="/api/file/upload/avatar"
    :headers="getHeaders"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" alt=""/>
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
</template>

<script lang="ts" setup>
import {ref, defineEmits, defineProps, onMounted, computed} from "vue";
import {getToken} from "@/util/token";

let imageUrl = ref();
const emits = defineEmits(["filePath"]);
const props = defineProps({
  imageUrl: {
    type: String,
    default: null,
  },
});
let getHeaders =computed( () => {
  return {
    "token": getToken(),
    "user-type":'admin'
  };
})
function handleAvatarSuccess(res) {
  imageUrl.value = res.data;
  emits("filePath", res.data);
}
function beforeAvatarUpload() {}
onMounted(()=>{
  if (props.imageUrl){
    imageUrl.value=props.imageUrl
  }
})
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
